﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace Paint
{
    public class DrawTriangleStrategy : IDrawStrategy 
    {
        private bool _canDraw = false;
        private Point _previous;
        private Polygon triangle_poly = null;
        public int num = 0;

        private BrushSettings _brushSettings;

        public DrawTriangleStrategy(BrushSettings settings)
        {
            _brushSettings = settings;
        }

		public void OnMouseDown(Canvas canvas, Point point)
        {
			_previous = point;
			_canDraw = true;
		}

        public void OnMouseMove(Canvas canvas, Point point)
        {
            if(_canDraw == false)
                return;

            Point point1, point2, point3;
            PointCollection polygonPoints=null;

            if (triangle_poly is null)
            {
                triangle_poly = new Polygon();
                triangle_poly.StrokeThickness = _brushSettings.StrokeThickness;
                triangle_poly.Stroke = _brushSettings.Color;
                triangle_poly.Points = polygonPoints;
                triangle_poly.Stretch = Stretch.Uniform;

                canvas.Children.Add(triangle_poly);
                num++;
            }

            point1 = new Point(_previous.X, _previous.Y);
            point2 = new Point(_previous.X - Math.Abs(_previous.X - point.X), point.Y);
            point3 = new Point(_previous.X + Math.Abs(_previous.X - point.X), point.Y);

            polygonPoints = new PointCollection();
            polygonPoints.Add(point1);
            polygonPoints.Add(point2);
            polygonPoints.Add(point3);

            triangle_poly.Points = polygonPoints;

            if (point.X >= _previous.X)
            {
                Canvas.SetLeft(triangle_poly, _previous.X-(point.X-_previous.X));
            }
            else
            {
                Canvas.SetLeft(triangle_poly, point.X);
            }

            if (point.Y >= _previous.Y)
            {
                Canvas.SetTop(triangle_poly, _previous.Y);
            }
            else
            {
                Canvas.SetTop(triangle_poly, point.Y);
            }
        }

        public void OnMouseUp(Canvas canvas, Point point)
        {
            _canDraw = false;
            triangle_poly = null;
            _previous = point;
            num = 0;
        }
    }
}
